package com.springcloud.productor01.controller;

import com.alibaba.fastjson.JSONObject;
import com.springcloud.productor01.dto.UserInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@RefreshScope
public class UserController {

    @Value("${system.expireTime}")
    private Integer expireTime;

    @Value("${server.port}")
    private Integer port;


    @GetMapping("/info")
    public JSONObject getUserInfo(String username) {

        JSONObject obj = new JSONObject();
        obj.put("code", 200);

        JSONObject data = new JSONObject();
        data.put("username", username);
        data.put("password", "123456");
        data.put("expireTime", expireTime);
        data.put("port", port);

        obj.put("data", data);

        return obj;
    }

    @PostMapping("/add")
    public JSONObject addUser(@RequestBody UserInfo userInfo) {
        JSONObject obj = new JSONObject();
        obj.put("code", 200);
        obj.put("msg", "添加成功");
        obj.put("info", userInfo);
        obj.put("port", port);

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return obj;
    }
}
